package com.cbgolf.oa.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.entity.ShopBean;
import com.cbgolf.oa.util.TextUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lq on 2017/11/1.
 */

public class RightDetailsAdapter extends BaseAdapter {

  private Context context;
  private List<ShopBean.goodsDetails> list = new ArrayList<>();

  public interface INumWorker {

    void updataNum(ShopBean.goodsDetails details, int num, int[] loction);
  }

  private INumWorker numWorker;

  public RightDetailsAdapter(Context context, List<ShopBean.goodsDetails> list, INumWorker worker) {
    this.context = context;
    this.list = list;
    if (null == this.list) {
      this.list = new ArrayList<>();
    }
    this.numWorker = worker;
  }

  @Override
  public int getCount() {
    return list.size();
  }

  @Override
  public Object getItem(int position) {
    return list.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(final int position, View view, ViewGroup parent) {
    Hder h = null;
    if (null == view) {
      h = new Hder();
      view = LayoutInflater.from(context).inflate(R.layout.item_right_details, null);
      h.nameTv = (TextView) view.findViewById(R.id.item_right_details_name_tv);
      h.priceTv = (TextView) view.findViewById(R.id.item_right_details_price_tv);
      h.numTv = (TextView) view.findViewById(R.id.item_right_details_num_tv);
      h.reduceView = view.findViewById(R.id.item_right_details_reduce_ll);
      h.addView = view.findViewById(R.id.item_right_details_add_ll);
      h.reduceIv = (ImageView) view.findViewById(R.id.item_right_details_reduce_iv);
      h.addIv = (ImageView) view.findViewById(R.id.item_right_details_add_iv);
      view.setTag(h);
//            AutoUtil.auto(view);
    } else {
      h = (Hder) view.getTag();
    }
    final ShopBean.goodsDetails m = list.get(position);
    if (null == m) {
      return view;
    }
    h.nameTv.setText(TextUtil.Text(m.goodName) + "   一" + TextUtil.Text(m.standardName));
    h.priceTv.setText(TextUtil.textNumFloat(m.parValue));

    final Hder finalH = h;
    h.reduceView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        int num = m.num;
        if (num > 0) {
          num--;
          m.num = num;
          finalH.numTv.setText(TextUtil.textNumInt(m.num));
          if (numWorker != null) {
//                        int[] loc = new int[2];
//                        finalH.reduceView.getLocationInWindow(loc);
            numWorker.updataNum(m, -1, null);
          }
        }
        if (num <= 0) {
          finalH.numTv.setVisibility(View.GONE);
          finalH.reduceView.setVisibility(View.GONE);
        }
      }
    });
    h.addView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        int num = m.num;
//                if (num == 99) {
//                    finalH.addView.setEnabled(false);
//                    return;
//                }
        num++;
        m.num = num;
        finalH.numTv.setText(TextUtil.textNumInt(m.num));
        if (View.GONE == finalH.numTv.getVisibility()) {
          finalH.numTv.setVisibility(View.VISIBLE);
        }
        if (View.GONE == finalH.reduceView.getVisibility()) {
          finalH.reduceView.setVisibility(View.VISIBLE);
        }
        if (numWorker != null) {
          int[] loc = new int[2];
          finalH.addView.getLocationInWindow(loc);
          numWorker.updataNum(m, 1, loc);
        }

//                if(mHolderClickListener!=null){
//                    int[] start_location = new int[2];
//                    viewHolder.shoppingNum.getLocationInWindow(start_location);
//                    //获取点击商品图片的位置
//                    //复制一个新的商品图标
//                    //TODO:解决方案，先监听到左边ListView的Item中，然后在开始动画添加
//                    mHolderClickListener.onHolderClick(drawable, start_location);
//                }
      }
    });
    h.reduceView.setVisibility(m.num > 0 ? View.VISIBLE : View.GONE);
    h.numTv.setVisibility(m.num > 0 ? View.VISIBLE : View.GONE);
    h.numTv.setText(TextUtil.textNumInt(m.num));
    return view;
  }

  class Hder {

    private TextView nameTv;
    private TextView priceTv;
    private TextView numTv;
    private View reduceView;
    private View addView;
    private ImageView reduceIv;
    private ImageView addIv;

  }

}
